package cn.schoolwow.workflow.module.task.action.service;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.module.common.handler.TransactionTryCatchFinallyHandler;
import cn.schoolwow.workflow.module.task.action.domain.CompleteTaskOption;
import cn.schoolwow.workflow.module.task.action.flow.complete.listener.ExecuteAfterCompleteTaskListenerFlow;
import cn.schoolwow.workflow.module.task.action.flow.complete.listener.ExecuteAfterTaskCreateListenerFlow;
import cn.schoolwow.workflow.module.task.action.flow.complete.listener.ExecuteBeforeCompleteTaskListenerFlow;
import cn.schoolwow.workflow.module.task.action.flow.complete.step.QueryCompleteTaskInstanceFlow;
import cn.schoolwow.workflow.module.task.action.flow.complete.step.QueryCompleteTaskNextNodeFlow;
import cn.schoolwow.workflow.module.task.action.flow.complete.step.taskType.HandleAddBeforeSignTaskFlow;
import cn.schoolwow.workflow.module.task.action.flow.complete.step.taskType.HandleAfterBeforeSignTaskFlow;
import cn.schoolwow.workflow.module.task.action.flow.complete.transaction.AddCompleteTaskHistoryFlow;
import cn.schoolwow.workflow.module.task.action.flow.complete.transaction.InsertWorkFlowTaskFlow;
import cn.schoolwow.workflow.module.task.action.flow.complete.transaction.UpdateWorkFlowInstanceAndTaskFlow;
import cn.schoolwow.workflow.module.task.action.flow.complete.transaction.UpdateWorkFlowInstanceTaskFlow;
import cn.schoolwow.workflow.module.task.action.flow.option.HandleIntelligentOptionFlow;

public class CompleteTaskServiceFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        flowContext.putInstanceData(new CompleteTaskOption());

        //查询工作流任务,实例和定义
        flowContext.executeFlowList(new QueryCompleteTaskInstanceFlow());
        //根据任务类型处理
        flowContext.executeFlowList(
                new HandleAddBeforeSignTaskFlow(),
                new HandleAfterBeforeSignTaskFlow()
        );
        //执行完成任务前监听器
        flowContext.executeFlowList(new ExecuteBeforeCompleteTaskListenerFlow());
        //查询当前节点的下一个节点
        flowContext.executeFlowList(new QueryCompleteTaskNextNodeFlow());
        //处理下一个节点
        flowContext.startFlow("完成任务-处理下一个节点")
                .tryCatchFinallyHandler(new TransactionTryCatchFinallyHandler())
                .next(new AddCompleteTaskHistoryFlow())
                .next(new UpdateWorkFlowInstanceTaskFlow())
                //处理单个节点
                .next(new InsertWorkFlowTaskFlow())
                .next(new UpdateWorkFlowInstanceAndTaskFlow())
                //处理智能去重选项
                .next(new HandleIntelligentOptionFlow())
                .execute();
        //执行完成任务后监听器
        flowContext.executeFlowList(
                new ExecuteAfterTaskCreateListenerFlow(),
                new ExecuteAfterCompleteTaskListenerFlow()
        );
    }

    @Override
    public String name() {
        return "完成工作流实例任务服务流程";
    }

}
